package org.tictactoe;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.*;
import org.tictactoe.*;
import org.tictactoe.corba.*;
import org.omg.CORBA.*;
import java.util.Scanner;

public class TictactoeClient {
	static TictactoeService ServiceImpl;
	private static Scanner scanner;
	
	public static void main(String[] args) {
	    try {
	      // Initialize the ORB.
	      org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	      // Obtain the root context.
	      org.omg.CORBA.Object rootObj = orb.resolve_initial_references("NameService");
	      NamingContextExt root = NamingContextExtHelper.narrow(rootObj);
	      // Locate an account manager through the Naming Service.
	      // Note that casting is needed as a workaround for a JDK 1.1.x bug.
	      org.omg.CORBA.Object mgrObj = ((NamingContext)root).resolve(root.to_name("TictactoeService"));
	      TictactoeService manager =  TictactoeServiceHelper.narrow(mgrObj);
	      System.out.println("Connection - OK!");
	      
	      int[][] board = new int[3][3];
	      for (int i=0; i<3; i++)
				for (int j=0; j<3; j++)
					board[i][j] = 0; 
	      IntHolder ret_team = new IntHolder();
		  Game game = manager.startGame(ret_team);
		  int team = ret_team.value;
		  int gameId = game.getId();
		  
		  if (team == 1) System.out.println("You play crosses.");
		  if (team == 2) System.out.println("You play noughts.");
		  
		  System.out.println("Waiting for opponent");
		  while (!game.isStarted()) {
			  try {
					Thread.sleep(500);
					continue;
				} catch (InterruptedException e) {
					break;
				}
		  }
		  System.out.println("Game on!");
	      
		  while (game.isEnded() == 0) {
			  Step step = game.getLastStep();
			  if (step.team != team) {
				  if ((step.col != -1) && (step.row != -1)) {
					  board[step.col][step.row] = step.team;
					  System.out.println("Opponent step - (" + step.col + ", " + step.row + ")");
				  }
				  
				  drawBoard(board);
				  
				  boolean correct;
				  do {
				  System.out.println("Your step: ");
				  int row = scanner.nextInt();
				  int col = scanner.nextInt();
				  Step myStep = new Step(row, col, team);
				  correct = game.makeStep(myStep);
				  if (!correct) System.out.println("Wrong enter! Repeat please."); 
				  } while (!correct);
				  
				  drawBoard(board);
			  }
			  Thread.sleep(100);
		  }
		  if (game.isEnded() == team) System.out.println("You win!");
		  else if (game.isEnded() == 3) System.out.println("Tie!");
		  else System.out.println("You lose :(");
		  manager.endGame(gameId);
		  
	   }
	   catch (Exception e) {
		   e.printStackTrace();
	   }
	    
	    
	}
	
	private static void drawBoard(int[][] board) {
		for (int i=0; i<3; i++) {
			  for (int j=0; j<3; j++) {
				  if (board[i][j] == 0) System.out.print(" _ ");
				  if (board[i][j] == 0) System.out.print(" x ");
				  if (board[i][j] == 0) System.out.print(" o ");
			  }
			  System.out.println();
		  }
	}
	
}
